package com.tomvian.screens.transitions 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import grim.framework.layout.Layout;
	import com.tomvian.screens.AbstractScreen;
	import com.tomvian.screens.AbstractTransition;
	import org.libspark.betweenas3.BetweenAS3;
	import org.libspark.betweenas3.core.easing.IEasing;
	import org.libspark.betweenas3.tweens.ITween;
	
	/**
	 * FadeOutInCover
	 * @author Tom Vian
	 */
	public class FadeOutInCover extends AbstractTransition
	{
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		protected var _tween:ITween;
		
		protected var _alphaNone:Object;
		protected var _alphaFull:Object;
		
		protected var _timeOut:Number;
		protected var _timeIn:Number;
		
		protected var _easingOut:IEasing;
		protected var _easingIn:IEasing;
		
		protected var _cover:Bitmap;
		
		//--------------------------------------------------------------------------
		//
		//  Getters / Setters
		//
		//--------------------------------------------------------------------------
		
		//--------------------------------------------------------------------------
		//
		//  Constructor
		//
		//--------------------------------------------------------------------------
		
		public function FadeOutInCover(timeOut:Number = 1.0, timeIn:Number = 1.0, easingOut:IEasing = null, easingIn:IEasing = null) 
		{
			_timeOut = timeOut;
			_timeIn = timeIn;
			_easingOut = easingOut;
			_easingIn = easingIn;
			
			_alphaNone = { alpha:0.0 };
			_alphaFull = { alpha:1.0 };
			
			_cover = new Bitmap(new BitmapData(1, 1, false, 0));
			_cover.width = Layout.stageWidth;
			_cover.height = Layout.stageHeight;
			_cover.alpha = 0.0;
		}
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		override public function play(fromScreen:AbstractScreen, toScreen:AbstractScreen):void 
		{
			super.play(fromScreen, toScreen);
			
			if (_tween) _tween.stop();
			
			screenManager.foreground = _cover;
			
			_tween = BetweenAS3.serial(	BetweenAS3.tween(_cover, _alphaFull, _alphaNone, _timeOut, _easingOut),
										BetweenAS3.func(swap), 
										BetweenAS3.tween(_cover, _alphaNone, _alphaFull, _timeIn, _easingIn));
			_tween.onComplete = onTransitionComplete;
			_tween.play();
		}
		
		override protected function finishTransition():void 
		{
			super.finishTransition();
			
			screenManager.foreground = null;
		}
	}
}